<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
            box-sizing: border-box;
        }

        .box {
            width: 300px;
            height: 200px;
            border: 1px solid #000;
            margin: auto;
        }

        .box>.head {
            display: flex;
            justify-content: space-between;
        }

        .box>.head>li {
            flex-grow: 1;
            text-align: center;
            cursor: pointer;
        }

        .active {
            background-color: #87ceeb;
        }

        .main {
            position: relative;
            background-color: red;
        }

        .main>li {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 178px;
            background-color: #87ceeb;
        }

        .mainActive {
            z-index: 999;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul class="head">
            <li class="active">标题一</li>
            <li>标题二</li>
            <li>标题三</li>
        </ul>

        <ul class="main">
            <li class="mainActive">内容一</li>
            <li>内容二</li>
            <li>内容三</li>
        </ul>
    </div>

    <script>
        //方法一：最开始写的不正规
        // let li = document.querySelectorAll("li")
        
        // for(let i = 0;i < 3;i++){
        //     li[i].onclick = function () {
        //         for(let i = 0;i < li.length;i++){
        //             li[i].className = null
        //         }
        //         li[i].className = "active"
        //         li[i + 3].className = "mainActive"
        //     }
        // }


        //方法二正规
        let li_list1 = document.querySelectorAll(".head>li")
        let li_list2 = document.querySelectorAll(".main>li")
        for(let i = 0;i < li_list1.length;i++){
            li_list1[i].onclick = function () {
                for(let j = 0;j < li_list2.length;j++){
                    li_list1[j].className = null
                    li_list2[j].className = null
                }
                li_list1[i].className = "active"
                li_list2[i].className = "mainActive"
            }
        }
    </script>
</body>

</html>